package com.bop.dehong.cart;


import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;

import com.base.dehong.util.LogUtils;
import com.base.dehong.util.NetWorkUtils;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseFragment;
import com.bop.dehong.base.TopManager;
import com.bop.dehong.config.Config;
import com.bop.dehong.webview.JavaScriptObject;


public class ShoppingCartActivity extends BaseFragment {
	private WebView mWebView;
	private String url;
	
	@Override
	public void initData() {
		mWebView.loadUrl(url);
	}

	@SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" }) @Override
	protected void initUI() {
		url = Config.getIP() + Config.zhongchou;
		LogUtils.e("请求weburl==" + url);
		mWebView = getView(R.id.webview);  
		/**
		 * 打开网页时不调用系统浏览器， 而是在本WebView中显示
		 */
		mWebView.setWebViewClient(new WebViewClient(){
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
			
			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				// TODO Auto-generated method stub
				ToastUtils.show(ShoppingCartActivity.this.getActivity(), "网络异常，请稍后再试");
				mWebView.loadUrl("about:blank");
				disappearProgressDialog();
				super.onReceivedError(view, errorCode, description, failingUrl);
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
				disappearProgressDialog();
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// TODO Auto-generated method stub
				super.onPageStarted(view, url, favicon);
				showProgressDialog();
			}
			
		});
		WebSettings webSettings = mWebView .getSettings();       
		webSettings.setJavaScriptEnabled(true); 
		webSettings.setSupportZoom(true);
		//打开页面时， 自适应屏幕：
		webSettings.setUseWideViewPort(true);//设置此属性，可任意比例缩放
		webSettings.setLoadWithOverviewMode(true);
		webSettings.setRenderPriority(RenderPriority.HIGH); 
		if (NetWorkUtils.isConnect(getActivity())) {// 有网络
			webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);  //设置 缓存模式  
		}else {
			webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
		}
        // 开启 DOM storage API 功能  
		webSettings.setDomStorageEnabled(true);  
        //开启 database storage API 功能  
		webSettings.setDatabaseEnabled(true);   
        String cacheDirPath = getActivity().getCacheDir().getAbsolutePath();  
  //      String cacheDirPath = getCacheDir().getAbsolutePath()+Constant.APP_DB_DIRNAME;  
        LogUtils.e("cachepath", "cacheDirPath=" + cacheDirPath);  
        //设置数据库缓存路径  
        webSettings.setDatabasePath(cacheDirPath);  
        //设置  Application Caches 缓存目录  
        webSettings.setAppCachePath(cacheDirPath);  
        //开启 Application Caches 功能  
        webSettings.setAppCacheEnabled(true);
		mWebView.addJavascriptInterface(new JavaScriptObject(this.getActivity()),"android");
	}

	@Override
	protected void initTop() {
		mPageName = getClass().getName();
		View mView = getView(R.id.top);
		TopManager manager = new TopManager(mView);
		manager.setTitle("德众筹");
	}
	

	@Override
	protected int getContentViewId() {
		return R.layout.activity_shopping_cart;
	}

	@Override
	public void onClick(View v)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		// TODO Auto-generated method stub
		
	}
	
}
